/************************************************************************************/
/*  Program name CAPGLEXT_CODE_COMB.sql                                             */
/*  Creater:  Karen Nip (PCCW - UniHub)                                             */
/*************   History   *************************                                */
/* 9 JAN, 2005   Karen Nip         First created                                    */
/************************************************************************************/
SET VERIFY OFF
ALTER SESSION SET NLS_DATE_FORMAT='YYYYMMDD HH24:MI:SS';
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPGLEXT_CODE_COMB';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	lv_FileName		VARCHAR2(100)	:=	'CAPGL_CODE_COMB.dat';
	lv_PathName		VARCHAR2(100)	:=	'/tmp';
	lv_UTL_File		UTL_FILE.File_Type;

	lv_NGO_Name		VARCHAR2(100);
	lv_NGO_Count	NUMBER;

	CURSOR Cur_F(p_NGO VARCHAR2) IS
	SELECT 
		GCC.*
	FROM   
		GL_CODE_COMBINATIONS	GCC
    ,	GL_SETS_OF_BOOKS		SOB
	WHERE	GCC.CHART_OF_ACCOUNTS_ID	=	SOB.CHART_OF_ACCOUNTS_ID
	AND		SUBSTR(SOB.SHORT_NAME,5,4)	=	p_NGO;

BEGIN
	lv_NGO_Name		:=	UPPER('&1');

	SELECT	COUNT('A')
	INTO	lv_NGO_Count
	FROM	HR_ALL_ORGANIZATION_UNITS
	WHERE	SUBSTR(Name,4,4)	=	lv_NGO_Name;

	IF	lv_NGO_Count	>=	1	THEN
		lv_UTL_File		:=	UTL_FILE.FOpen(lv_PathName, lv_FileName, 'w');
		FOR C1 IN Cur_F(lv_NGO_Name) LOOP
			UTL_FILE.PUT_LINE(
				lv_UTL_File,
				C1.CODE_COMBINATION_ID||CHR(9)||
				C1.LAST_UPDATE_DATE||CHR(9)||
					C1.LAST_UPDATED_BY||CHR(9)||
					C1.CHART_OF_ACCOUNTS_ID||CHR(9)||
				C1.DETAIL_POSTING_ALLOWED_FLAG||CHR(9)||
				C1.DETAIL_BUDGETING_ALLOWED_FLAG||CHR(9)||
				C1.ACCOUNT_TYPE||CHR(9)||
				C1.ENABLED_FLAG||CHR(9)||
				C1.SUMMARY_FLAG||CHR(9)||
				C1.SEGMENT1||CHR(9)||
				C1.SEGMENT2||CHR(9)||
				C1.SEGMENT3||CHR(9)||
				C1.SEGMENT4||CHR(9)||
				C1.SEGMENT5||CHR(9)||
				C1.SEGMENT6||CHR(9)||
				C1.SEGMENT7||CHR(9)||
				C1.SEGMENT8||CHR(9)||
				C1.SEGMENT9||CHR(9)||
				C1.SEGMENT10||CHR(9)||
				C1.SEGMENT11||CHR(9)||
				C1.SEGMENT12||CHR(9)||
				C1.SEGMENT13||CHR(9)||
				C1.SEGMENT14||CHR(9)||
				C1.SEGMENT15||CHR(9)||
				C1.SEGMENT16||CHR(9)||
				C1.SEGMENT17||CHR(9)||
				C1.SEGMENT18||CHR(9)||
				C1.SEGMENT19||CHR(9)||
				C1.SEGMENT20||CHR(9)||
				C1.SEGMENT21||CHR(9)||
				C1.SEGMENT22||CHR(9)||
				C1.SEGMENT23||CHR(9)||
				C1.SEGMENT24||CHR(9)||
				C1.SEGMENT25||CHR(9)||
				C1.SEGMENT26||CHR(9)||
				C1.SEGMENT27||CHR(9)||
				C1.SEGMENT28||CHR(9)||
				C1.SEGMENT29||CHR(9)||
				C1.SEGMENT30||CHR(9)||
				C1.DESCRIPTION||CHR(9)||
				C1.TEMPLATE_ID||CHR(9)||
				C1.ALLOCATION_CREATE_FLAG||CHR(9)||
				C1.START_DATE_ACTIVE||CHR(9)||
				C1.END_DATE_ACTIVE||CHR(9)||
				C1.ATTRIBUTE1||CHR(9)||
				C1.ATTRIBUTE2||CHR(9)||
				C1.ATTRIBUTE3||CHR(9)||
				C1.ATTRIBUTE4||CHR(9)||
				C1.ATTRIBUTE5||CHR(9)||
				C1.ATTRIBUTE6||CHR(9)||
				C1.ATTRIBUTE7||CHR(9)||
				C1.ATTRIBUTE8||CHR(9)||
				C1.ATTRIBUTE9||CHR(9)||
				C1.ATTRIBUTE10||CHR(9)||
				C1.CONTEXT||CHR(9)||
				C1.SEGMENT_ATTRIBUTE1||CHR(9)||
				C1.SEGMENT_ATTRIBUTE2||CHR(9)||
				C1.SEGMENT_ATTRIBUTE3||CHR(9)||
				C1.SEGMENT_ATTRIBUTE4||CHR(9)||
				C1.SEGMENT_ATTRIBUTE5||CHR(9)||
				C1.SEGMENT_ATTRIBUTE6||CHR(9)||
				C1.SEGMENT_ATTRIBUTE7||CHR(9)||
				C1.SEGMENT_ATTRIBUTE8||CHR(9)||
				C1.SEGMENT_ATTRIBUTE9||CHR(9)||
				C1.SEGMENT_ATTRIBUTE10||CHR(9)||
				C1.SEGMENT_ATTRIBUTE11||CHR(9)||
				C1.SEGMENT_ATTRIBUTE12||CHR(9)||
				C1.SEGMENT_ATTRIBUTE13||CHR(9)||
				C1.SEGMENT_ATTRIBUTE14||CHR(9)||
				C1.SEGMENT_ATTRIBUTE15||CHR(9)||
				C1.SEGMENT_ATTRIBUTE16||CHR(9)||
				C1.SEGMENT_ATTRIBUTE17||CHR(9)||
				C1.SEGMENT_ATTRIBUTE18||CHR(9)||
				C1.SEGMENT_ATTRIBUTE19||CHR(9)||
				C1.SEGMENT_ATTRIBUTE20||CHR(9)||
				C1.SEGMENT_ATTRIBUTE21||CHR(9)||
				C1.SEGMENT_ATTRIBUTE22||CHR(9)||
				C1.SEGMENT_ATTRIBUTE23||CHR(9)||
				C1.SEGMENT_ATTRIBUTE24||CHR(9)||
				C1.SEGMENT_ATTRIBUTE25||CHR(9)||
				C1.SEGMENT_ATTRIBUTE26||CHR(9)||
				C1.SEGMENT_ATTRIBUTE27||CHR(9)||
				C1.SEGMENT_ATTRIBUTE28||CHR(9)||
				C1.SEGMENT_ATTRIBUTE29||CHR(9)||
				C1.SEGMENT_ATTRIBUTE30||CHR(9)||
				C1.SEGMENT_ATTRIBUTE31||CHR(9)||
				C1.SEGMENT_ATTRIBUTE32||CHR(9)||
				C1.SEGMENT_ATTRIBUTE33||CHR(9)||
				C1.SEGMENT_ATTRIBUTE34||CHR(9)||
				C1.SEGMENT_ATTRIBUTE35||CHR(9)||
				C1.SEGMENT_ATTRIBUTE36||CHR(9)||
				C1.SEGMENT_ATTRIBUTE37||CHR(9)||
				C1.SEGMENT_ATTRIBUTE38||CHR(9)||
				C1.SEGMENT_ATTRIBUTE39||CHR(9)||
				C1.SEGMENT_ATTRIBUTE40||CHR(9)||
				C1.SEGMENT_ATTRIBUTE41||CHR(9)||
				C1.SEGMENT_ATTRIBUTE42||CHR(9)||
				C1.REFERENCE1||CHR(9)||
				C1.REFERENCE2||CHR(9)||
				C1.REFERENCE3||CHR(9)||
				C1.REFERENCE4||CHR(9)||
				C1.REFERENCE5||CHR(9)||
				C1.JGZZ_RECON_FLAG||CHR(9)||
				C1.JGZZ_RECON_CONTEXT||CHR(9)||
				C1.PRESERVE_FLAG||CHR(9)||
				C1.REFRESH_FLAG||CHR(9)||
				C1.IGI_BALANCED_BUDGET_FLAG||CHR(9)||
				C1.COMPANY_COST_CENTER_ORG_ID||CHR(9)||
				C1.REVALUATION_ID||CHR(9)||
				CAPBREXT_COMMON.GetUser(C1.LAST_UPDATED_BY)||CHR(9)||
				CAPBREXT_COMMON.GetFlexStrCode(C1.CHART_OF_ACCOUNTS_ID)
			);

			lv_Scc_Count	:=	lv_Scc_Count	+	1;
		END LOOP;
		UTL_FILE.FClose(lv_UTL_File);
	ELSE
		lv_Err_Count	:=	lv_Err_Count + 1;
		lv_Err_Msg		:=	lv_NGO_Name||': No code combinations found for this NGO code';
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/